package threadPool;

import java.util.concurrent.*;
//线程池最大值该如何设置
//io密集型  》判断程序中十分耗IO的线程，大约是线程数的两倍，这样才能有剩下的线程去跑其他的程序
//CPU密集型 》根据电脑的CPU核数来判断  Runtime.getRuntime().availableProcessors()
public class ThreadPoolExcuterDemo {
    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                Runtime.getRuntime().availableProcessors(),//cpu核数
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(4),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        try {
            for (int i = 0; i < 9; i++) {
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName() + " 处理ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}
